package com.whut.monitor.netty;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.math.RoundingMode;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DecimalFormat;
import java.util.Date;

//服务端设计思路：服务端接收客户端的请求（阻塞式），
// 开始写入流，将文件写到服务器的指定目录下，并与传输过来的文件同名。
public class FileTransferServer extends ServerSocket {
    private static final int SERVER_PORT = 8089; // 服务端端口
    private ServerSocket serverSocket;
    private DataInputStream dis;
    private FileOutputStream fos;
    private static DecimalFormat df = null;

    static {
        // 设置数字格式，保留一位有效小数
        df = new DecimalFormat("#0.0");
        df.setRoundingMode(RoundingMode.HALF_UP);
        df.setMinimumFractionDigits(1);
        df.setMaximumFractionDigits(1);
    }

    public FileTransferServer() throws Exception {
        super(SERVER_PORT);
        this.serverSocket = this;
    }

    public void load() throws Exception {
            Socket socket = serverSocket.accept();
            try {
                dis = new DataInputStream(socket.getInputStream());

                // 文件名和长度
                String fileName = dis.readUTF();
                long fileLength = dis.readLong();
                File directory = new File("/usr/local/FTCache");
                if(!directory.exists()) {
                    directory.mkdir();
                }
                File file = new File(directory.getAbsolutePath() + File.separatorChar +new Date().getTime() +fileName);
                fos = new FileOutputStream(file);

                // 开始接收文件
                byte[] bytes = new byte[1024];
                int length = 0;
                while((length = dis.read(bytes, 0, bytes.length)) >0) {
                    fos.write(bytes, 0, length);
                    fos.flush();
                }
                System.out.println("======== 文件接收成功 [File Name：" + fileName + "] [Size：" + getFormatFileSize(fileLength) + "] ========");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(fos != null)
                        fos.close();
                    if(dis != null){
                        dis.close();
                        socket.close();
                        serverSocket.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    }


    /**
     * 格式化文件大小
     * @param length
     * @return
     */
    private String getFormatFileSize(long length) {
        double size = ((double) length) / (1 << 30);
        if(size >= 1) {
            return df.format(size) + "GB";
        }
        size = ((double) length) / (1 << 20);
        if(size >= 1) {
            return df.format(size) + "MB";
        }
        size = ((double) length) / (1 << 10);
        if(size >= 1) {
            return df.format(size) + "KB";
        }
        return length + "B";
    }

    /**
     * 入口
     * @param args
     */
//    public static void main(String[] args) {
//        try {
//            FileTransferServer server = new FileTransferServer(); // 启动服务端
//            server.load();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}